import QtQuick 2.15
import QtQuick.Controls 2.15
import './../../../controls'
//手环信息
Rectangle {
    id:root
    anchors.fill: parent
    gradient: mainGradient
    property var orderInfo: null
    property string bno: ''
    property string bname: ''
    property var totalAount:0.0 //合计金额
    property int payType: {
        if(orderInfo && 'de_status' in orderInfo){
            switch(orderInfo.de_status)
            {
            case 0:
                return 0//没交押金
            case 1:
                return 1 //已经交押金 开始结算
            case 2:
                return -1
            case 3:
                return -1
            }
        }
        return -1
    }
    Component.onCompleted: {
        loadData()
    }

    function loadData()
    {
        var request = HttpHelper.createIMRequest();
        //        request.enabledLog=true
        request.onSucceed( function(request){
            console.log('----water_query_info_bno---',JSON.stringify(request.responseData))
            orderInfo=request.responseData
            lvMode.clear()
            totalAount=0;
            for(let i=0;i<orderInfo.bracelets.length;i++)
            {
                let bracelet=orderInfo.bracelets[i]
                bracelet.checked=false
                if(bracelet.b_no === root.bno)
                {
                    root.bname=bracelet.b_name
                }

                totalAount+=bracelet.total
                lvMode.append(bracelet )
            }
        } );
        request.onError(  function(request){
            window.clearAll()
            CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_query_info_bno/"+bno)
    }

    function payCallBack(bno){
        loadData()
        // if(payType === 0)
        // {
        //     //交押金

        // }
        // if(payType === 1)
        // {
        //     //结算
        // }

        console.log('---------pay successed------',son)
    }
    Rectangle{
        id:rectRoot
        width: parent.width * 0.8
        height: 640
        anchors.horizontalCenter: parent.horizontalCenter
        y:100
        radius: 20
        Item {
            anchors.fill: parent
            Row{
                id:rowTitle
                leftPadding: 30
                topPadding: 30
                spacing: 30
                Column{
                    spacing: 10
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("订单编号:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 's_no' in orderInfo)
                                    {
                                        return orderInfo.s_no
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("手机号码:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'phone' in orderInfo)
                                    {
                                        return orderInfo.phone
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("用户昵称:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'name' in orderInfo)
                                    {
                                        return orderInfo.name
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }

                }
                Column{
                    spacing: 10
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("下单时间:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'created_time' in orderInfo)
                                    {
                                        return orderInfo.created_time
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("手环编号:")
                            Text {
                                color: '#333333'
                                text: root.bname
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("支付状态:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'status' in orderInfo)
                                    {
                                        switch(orderInfo.status)
                                        {
                                        case 0:
                                            return qsTr('待支付')
                                        case 1:
                                            return qsTr('已支付')
                                        case 2:
                                            return qsTr('已使用')
                                        case 3:
                                            return qsTr('已取消')
                                        case 4:
                                            return qsTr('申请退款')
                                        case 5:
                                            return qsTr('已退款')
                                        case 6:
                                            return qsTr('已拒绝')
                                        }
                                        return qsTr('未知状态')
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                }
                Column{
                    spacing: 10
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("金额:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'price' in orderInfo)
                                    {
                                        return orderInfo.price
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("类型:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'part_title' in orderInfo){
                                        return  orderInfo.part_title
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                    Item {
                        width: rectRoot.width / 3
                        height: 30
                        Text {
                            width: 90
                            color: '#333333'
                            text: qsTr("支付时间:")
                            Text {
                                color: '#333333'
                                text: {
                                    if(orderInfo && 'pay_time' in orderInfo && orderInfo.pay_time)
                                    {
                                        var dateTime = new Date( orderInfo.pay_time * 1000)
                                        return Qt.formatDateTime(dateTime, "yyyy/MM/dd HH:mm:ss");
                                        // return orderInfo.pay_time
                                    }
                                    return ''
                                }
                                anchors.left: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }
                    }
                }
            }
            Rectangle{
                anchors.fill: parent
                anchors.topMargin: rowTitle.height + 50
                anchors.leftMargin: 30
                anchors.bottomMargin: 60
                anchors.rightMargin: 30
                border.color: '#dddddd'
                Text {
                    text: qsTr("手环列表")
                    anchors.bottom: parent.top
                    anchors.bottomMargin: 12
                }

                Item {
                    id:itemHeader
                    width: parent.width
                    height: 30
                    property var itemWidth: (width - 60) / repHeader.model.length
                    Row{
                        Rectangle{
                            width: 60
                            height: itemHeader.height
                            border.color: '#dddddd'
                        }

                        Repeater{
                            id:repHeader
                            model: ['手环号','手环编号','性别','绑定时间','类型','押金','押金方式','押金状态','手环状态','消费金额','支付状态','支付时间','操作']
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: itemHeader.height
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: modelData
                                    color: '#333333'
                                }
                            }
                        }
                    }
                }
                ListModel{
                    id:lvMode
                }

                ListView{
                    id:lv
                    anchors.fill: parent
                    anchors.topMargin: itemHeader.height
                    property var itemHeight: itemHeader.height * 1.5
                    clip: true
                    ScrollBar.vertical: ScrollBar{}
                    model:lvMode
                    delegate: Item {
                        width: lv.width
                        height: lv.itemHeight
                        Row{
                            Rectangle{
                                width: 60
                                height:lv.itemHeight
                                border.color: '#dddddd'
                                CheckBox{
                                    anchors.centerIn: parent
                                    width: 16
                                    height: 16
                                    indicator.width: 16
                                    indicator.height: 16
                                    checked: model.checked
                                    onCheckedChanged: model.checked = checked
                                }
                            }

                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: {
                                        if('b_name' in model && model.b_name)
                                        {
                                            return model.b_name
                                        }
                                        return ''
                                    }

                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.b_no
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.sex === 0 ? '男' : '女'
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.fill: parent
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    text: model.create_time
                                    color: '#333333'
                                    clip: true
                                    wrapMode: Text.WordWrap
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.type === 0 ? qsTr('成人') : qsTr('儿童')
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.deposit
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: {
                                        if(model.de_status === 0)
                                        {
                                            return ''
                                        }

                                        switch(model.deposit_pay_way)
                                        {
                                        case 0:
                                            return qsTr('微信')
                                        case 1:
                                            return qsTr('支付宝')
                                        case 2:
                                            return qsTr('现金')
                                        }
                                        return model.deposit_pay_way
                                    }
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: {
                                        switch(model.de_status)
                                        {
                                        case 0:
                                            return qsTr('待支付')
                                        case 1:
                                            return qsTr('已支付')
                                        case 2:
                                            return qsTr('已退款')
                                        case 3:
                                            return qsTr('已取消')
                                        }
                                        return model.de_status
                                    }
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.status === 0 ? qsTr('使用中') : qsTr('已归还')
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: model.total
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.centerIn: parent
                                    text: {
                                        switch(model.p_status)
                                        {
                                        case 0:
                                            return qsTr('未支付')
                                        case 1:
                                            return qsTr('已支付')
                                        case 2:
                                            return qsTr('已退款')
                                        case 3:
                                            return qsTr('已取消')
                                        }
                                        return model.p_status
                                    }

                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Text {
                                    anchors.fill: parent
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                    clip: true
                                    wrapMode: Text.WordWrap
                                    text: {
                                        if('pay_time' in model && model.pay_time)
                                        {
                                            return model.pay_time
                                        }
                                        return ''
                                    }
                                    color: '#333333'
                                }
                            }
                            Rectangle{
                                width: itemHeader.itemWidth
                                height: lv.itemHeight
                                border.color: '#dddddd'
                                Button{
                                    width: parent.width * 0.6
                                    height: 30
                                    anchors.centerIn: parent
                                    background: Item {
                                        Text {
                                            anchors.centerIn: parent
                                            color: 'blue'
                                            text: qsTr("详情")
                                        }
                                    }
                                    onClicked: {
                                        CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/SettlementInfoView.qml',{bno:model.b_no})
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Text {
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 20
                x:30
                color: 'red'
                font.pixelSize: 26
                font.bold: true
                text: qsTr("合计: ￥") + root.totalAount
            }
            Row{
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                spacing: 30
                Button{
                    width: 160
                    height: 40
                    enabled:  orderInfo !== null
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("支付押金")
                        }
                    }
                    onClicked: {
                        let bnos=[]
                        let samount=0.0
                        for(let i=0;i<lvMode.count;i++)
                        {
                            let item=lvMode.get(i)
                            if(item.checked)
                            {
                                if(item.de_status !== 0)
                                {
                                    CGlobal.showMessage(window,qsTr('请勾选待支付押金的手环'))
                                    return
                                }

                                bnos.push(item)
                                samount+=parseFloat(item.deposit)
                            }
                        }
                        if(bnos.length === 0)
                        {
                            CGlobal.showMessage(window,qsTr('请勾选待支付押金的手环'))
                            return
                        }
                        let bnoStr=''
                        let bnameStr=''
                        for(let i=0;i<bnos.length;i++)
                        {
                            bnoStr+=bnos[i].b_no
                            bnameStr+=bnos[i].b_name
                            if(i < bnos.length - 1)
                            {
                                bnoStr+=','
                                bnameStr+=','
                            }
                        }

                        var dlg=CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/PayDialog.qml',
                                                            {
                                                                amount:samount,
                                                                bno:bnoStr,
                                                                bname:bnameStr,
                                                                isdeposit:true,
                                                                callBackFunc:payCallBack
                                                            })
                    }
                }
                Button{
                    width: 160
                    height: 40
                    enabled:  orderInfo !== null
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("商品结算")
                        }
                    }
                    onClicked: {
                        let bnos=[]
                        let samount=0.0
                        for(let i=0;i<lvMode.count;i++)
                        {
                            let item=lvMode.get(i)
                            if(item.checked /*&& item.de_status === 1 && item.p_status === 0*/)
                            {
                                if(item.de_status !== 1 || item.p_status !== 0)
                                {
                                    CGlobal.showMessage(window,qsTr('请勾选已支付押金并且商品待支付的手环'))
                                    return
                                }
                                bnos.push(item)
                                samount+=parseFloat(item.total)
                            }
                        }
                        if(bnos.length === 0)
                        {
                            CGlobal.showMessage(window,qsTr('请勾选已支付押金并且商品待支付的手环'))
                            return
                        }
                        let bnoStr=''
                        let bnameStr=''
                        for(let i=0;i<bnos.length;i++)
                        {
                            bnoStr+=bnos[i].b_no
                            bnameStr+=bnos[i].b_name
                            if(i < bnos.length - 1)
                            {
                                bnoStr+=','
                                bnameStr+=','
                            }
                        }

                        var dlg=CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/PayDialog.qml',
                                                            {
                                                                amount:samount,
                                                                bno:bnoStr,
                                                                bname:bnameStr,
                                                                isdeposit:false,
                                                                callBackFunc:payCallBack
                                                            })
                    }
                }
                Button{
                    width: 160
                    height: 40
                    enabled:  orderInfo !== null
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("退还押金")
                        }
                    }
                    onClicked: {
                        let bnos=[]
                        let samount=0.0
                        for(let i=0;i<lvMode.count;i++)
                        {
                            let item=lvMode.get(i)
                            if(item.checked)
                            {
                                if(item.de_status !== 1)
                                {
                                    CGlobal.showMessage(window,qsTr('请勾选已支付押金并且已经结算完商品的手环'))
                                    return
                                }
                                if(item.p_status === 0 && item.total !== 0)
                                {
                                    CGlobal.showMessage(window,qsTr('请勾选已支付押金并且已经结算完商品的手环'))
                                    return
                                }
                                bnos.push({'bno':item.b_no,'payType':item.deposit_pay_way})
                                samount+=parseFloat(item.deposit)
                            }
                        }
                        if(bnos.length === 0)
                        {
                            CGlobal.showMessage(window,qsTr('请勾选已支付押金并且已经结算完商品的手环'))
                            return
                        }
                        var isBackSelf=false
                        let bnoStr=''
                        let lastPayTpye=bnos[0].payType
                        for(let i=0;i<bnos.length;i++)
                        {
                            if(bnos[i].payType !==  lastPayTpye)
                            {
                                CGlobal.showMessage(window,qsTr('押金支付类型不一致，无法退还'))
                                return
                            }
                            if(root.bno === bnos[i].bno)
                            {
                                isBackSelf=true
                            }

                            bnoStr+=bnos[i].bno
                            if(i < bnos.length - 1)
                            {
                                bnoStr+=','
                            }
                        }
                        if(!CommonHelper.question(window,qsTr('确定要退还押金吗?')))
                        {
                            return
                        }



                        var request = HttpHelper.createIMRequest();
                        // request.enabledLog=true
                        request.onSucceed( function(request){
                             CGlobal.showMessage(window,'退还成功')
                             if(isBackSelf)
                             {
                                window.clearAll()
                             }
                             else
                             {
                                loadData()
                             }
                        } );
                        request.onError(  function(request){
                            CGlobal.showMessage(window,request.errorMessage)
                        } );
                        request.post("/api/common/refund_micropay_order",
                                     {
                                         bno:bnoStr,
                                         type:lastPayTpye
                                     })

                    }
                }
                Button{
                    width: 160
                    height: 40
                    enabled:  orderInfo !== null
                    background: Rectangle{
                        radius: 10
                        color: '#EA9508'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: qsTr("退还手环")
                        }
                    }
                    onClicked: {
                        let bnos=[]
                        for(let i=0;i<lvMode.count;i++)
                        {
                            let item=lvMode.get(i)
                            if(item.checked &&item.de_status === 1)
                            {
                                CGlobal.showMessage(window,qsTr('请勾选已退还押金或未支付押金的手环'))
                                return
                            }
                            if(item.checked)
                            {
                                bnos.push(item.b_no)
                            }
                        }
                        if(bnos.length === 0)
                        {
                            CGlobal.showMessage(window,qsTr('请勾选已退还押金或未支付押金的手环'))
                            return
                        }
                        let bnoStr=''
                        for(let i=0;i<bnos.length;i++)
                        {
                            bnoStr+=bnos[i]
                            if(i < bnos.length - 1)
                            {
                                bnoStr+=','
                            }
                        }
                        if(!CommonHelper.question(window,qsTr('确定要退还手环吗?')))
                        {
                            return
                        }

                        var request = HttpHelper.createIMRequest();
                        // request.enabledLog=true
                        request.onSucceed( function(request){
                            CGlobal.showMessage(window,'退还成功')
                            loadData()
                        } );
                        request.onError(  function(request){
                            CGlobal.showMessage(window,request.errorMessage)
                        } );
                        request.get("/api/common/return_by_bno?bno="+bnoStr)

                    }
                }
            }

        }
    }
}
